/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define LOG_TAG "TCAM.utils"
//#define LOG_NDEBUG 0

#include "Utils.h"
#include <android-base/logging.h>
#include <log/log.h>

namespace vendor {
namespace ecarx {
namespace xma {
namespace tcam {
namespace V1_0 {

void Utils::DateTime2Asn(const DateTime& local, ASN1T_DateTime& asn) {
    switch (local.dataOrTi) {
        case DateOrTime::None: {
            asn.dateOrTime = ::DateOrTi::dateOrTi_None;
            break;
        }
        case DateOrTime::Date: {
            asn.dateOrTime = ::DateOrTi::dateOrTi_Date;
            break;
        }
        case DateOrTime::Ti: {
            asn.dateOrTime = ::DateOrTi::dateOrTi_Ti;
            break;
        }
        case DateOrTime::Date_Ti: {
            asn.dateOrTime = ::DateOrTi::dateOrTi_DateTi;
            break;
        }
        default:
            ALOGE("unkonw dataOrTi!");
            asn.dateOrTime = ::DateOrTi::dateOrTi_None;
            break;
    }

    asn.year = (local.year < 2000 ? 2000 : local.year);
    asn.month = (local.month < 1 ? 1 : local.month);
    asn.day = (local.day < 1 ? 1 : local.day);
    asn.hour = local.hour;
    asn.minute = local.minute;
    asn.second = local.second;
}

void Utils::Asn2DateTime(const ASN1T_DateTime& asn, DateTime& local) {
    switch (asn.dateOrTime) {
        case ::DateOrTi::dateOrTi_None: {
            local.dataOrTi = DateOrTime::None;
            break;
        }
        case ::DateOrTi::dateOrTi_Date: {
            local.dataOrTi = DateOrTime::Date;
            break;
        }
        case ::DateOrTi::dateOrTi_Ti: {
            local.dataOrTi = DateOrTime::Ti;
            break;
        }
        case ::DateOrTi::dateOrTi_DateTi: {
            local.dataOrTi = DateOrTime::Date_Ti;
            break;
        }
        default:
            ALOGE("unkonw dateOrTime!");
            break;
    }
    local.year = asn.year;
    local.month = asn.month;
    local.day = asn.day;
    local.hour = asn.hour;
    local.minute = asn.minute;
    local.second = asn.second;
}

}  // namespace V1_0
}  // namespace tcam
}  // namespace xma
}  // namespace ecarx
}  // namespace vendor